package com.ereport.controller.projectsController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.AccountsDAO;
import com.ereport.dao.ExecutiveDAO;
import com.ereport.dao.ProjectsDAO;
import com.ereport.entity.AccContacts;
import com.ereport.entity.Accounts;
import com.ereport.entity.Executives;
import com.ereport.entity.Projects;

@Controller
@RequestMapping("/project")
public class ProjectController {
	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		model.setViewName("/jsp/projects.jsp");
		return model;
		
	}
	@RequestMapping("/save.do")
	public ModelAndView save(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		
		String orgname=req.getParameter("org");
		String indsType=req.getParameter("indsType");
		String nameOfPrj=req.getParameter("nameOfPrj");
		String endDate=req.getParameter("endDate");
		String prjctFor=req.getParameter("prjctFor");
		String typeOfPrj=req.getParameter("typeOfPrj");
		String division=req.getParameter("division");
		String nameOfUser=req.getParameter("nameOfUser");
		String strDate=req.getParameter("strDate");
		String ems=req.getParameter("ems");
		String usedComp=req.getParameter("usedComp");
		Date enddt=null;
		Date strdt=null;
		DateFormat format=new SimpleDateFormat("dd-MM-yyyy");
		if(!endDate.equals("")){
			try {
				enddt=format.parse(endDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			enddt=new java.sql.Date(enddt.getTime());
		}
		if(!strDate.equals("")){
			try {
				strdt=format.parse(strDate);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			strdt=new java.sql.Date(strdt.getTime());
		}
		Date createdDate=new Date();
		createdDate=new java.sql.Date(createdDate.getTime());
		int createdUser=ex.getId();
		ProjectsDAO pdao=new ProjectsDAO();

		if(req.getParameter("pid")!=null){
			int pid=Integer.parseInt(req.getParameter("pid"));
			try {
				pdao.insert(new Projects(pid,ex,orgname,indsType,nameOfPrj,prjctFor,typeOfPrj,division,nameOfUser,ems,usedComp,strdt,enddt,createdUser,createdDate));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
		try {
			pdao.insert(new Projects(ex,orgname,indsType,nameOfPrj,prjctFor,typeOfPrj,division,nameOfUser,ems,usedComp,strdt,enddt,createdUser,createdDate));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		model.setViewName("redirect:open.do");
		model.addObject("msg","Successfully Saved Project Details");
		return model;
		
	}
	@RequestMapping("/searchProj.do")
	public @ResponseBody
	String 	searchProj(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		
		ProjectsDAO pdao=new ProjectsDAO();
		List<Projects> prList=null;
		try {
			prList=pdao.getAllProj(ex.getId());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Iterator itr=prList.iterator();
		String pr="";
		while(itr.hasNext()){
			Object obj=itr.next();
			Projects prj=(Projects)obj;
			String projName=prj.getProjectName();
			int prid=prj.getId();
			pr+=prid+","+projName+"/";
		}
		
		return pr;
		
	}
	@RequestMapping("/testProj.do")
	public @ResponseBody
	String testProj(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		String projName=req.getParameter("prjName");
		ProjectsDAO pdao=new ProjectsDAO();
		Projects pr=null;
		try {
			pr = pdao.testProj(ex.getId(),projName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(pr!=null){
			return "Project Name Already Exist";
		}
		return "";
		
	}
@RequestMapping(value="/getProjDet.do",method = RequestMethod.POST)
public  ModelAndView getProjDet(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		String projName=req.getParameter("projName");
		int prid=0;
		if(!projName.equals("")){
			prid=Integer.parseInt(projName);
		}
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		ProjectsDAO prDao=new ProjectsDAO();
		Projects prj=null;
		try { 
			prj=prDao.getProj(ex.getId(),prid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(prj!=null){
			model.setViewName("/jsp/editProjects.jsp");
			model.addObject("prj",prj);
		
		return model;
		}
		return model;
	}
@RequestMapping(value="/openAdminProj.do")
public  ModelAndView openAdminProj(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
	ExecutiveDAO edao=new ExecutiveDAO();
	List<Executives> exlist=null;
	try {
		exlist=edao.getExecutives();
	} catch (Exception e) {
		e.printStackTrace();
	}
	ProjectsDAO prDao=new ProjectsDAO();
	List<Projects> prList=null;
	try {
		prList=prDao.findAll();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	if(prList!=null){
		model.setViewName("/jsp/admin/projectsInAdmin.jsp");
		model.addObject("prList",prList);
		model.addObject("exlist", exlist);
	return model;
	}
	return null;
	
}
@RequestMapping(value="/getPrjByExeId.do")
public  ModelAndView getPrjByExeId(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
	String exeid=req.getParameter("exeid");
	int exid=0;
	if(!exeid.equals("")){
		exid=Integer.parseInt(exeid);
	}
	
	ProjectsDAO prDao=new ProjectsDAO();
	List<Projects> prList=null;
	try {
		prList=prDao.findByExeId(exid);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	if(prList!=null){
		model.setViewName("/jsp/admin/projectsRes.jsp");
		model.addObject("prList",prList);
		return model;
	}
	return null;
	
}
@RequestMapping(value="/getPrjByExeIdAndProj.do")
public  ModelAndView getPrjByExeIdAndProj(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
	String exeid=req.getParameter("exeid");
	int exid=0;
	if(!exeid.equals("")){
		exid=Integer.parseInt(exeid);
	}
	String proj=req.getParameter("proj");
	ProjectsDAO prDao=new ProjectsDAO();
	Projects pr=null;
	try {
		pr=prDao.testProj(exid,proj);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	if(pr!=null){
		model.setViewName("/jsp/admin/projRes.jsp");
		model.addObject("prj",pr);
		return model;
	}
	return null;
	
}

}
